/*
    The MIT License

    Copyright (c) 2010 IFMO/GameDev Studio

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*/

#pragma once

/*-----------------------------------------------------------------------------
	FRScene shader :
-----------------------------------------------------------------------------*/


class EFRShader : public ICoreObject {
	public:
					EFRShader		( EFRScene *scene, EString name, lua_State *L, int idx );
					~EFRShader		( void );

		void		UpdateShader	( lua_State *L, int idx );
		void		LoadFXFile		( void );
	
		bool		BeginShading	( const EFREntity *entity, uint shading_group_id );
		void		EndShading		( void );
		
		EString		Name			( void ) { return name; }
		void		SetSkinning		( bool enable );
		void		SetJointXForm	( const EPoint &pos, const EQuaternion &orient );
		
	protected:
		class EFRScene	*scene;
		
		void		LoadResources	( void );
		
		void		SetUniform		( EFXHandle handle, float v );

		void		SetUniform		( EFXHandle handle, const EVector		v );
		void		SetUniform		( EFXHandle handle, const EPoint		p );
		void		SetUniform		( EFXHandle handle, const EQuaternion	q );
		void		SetUniform		( EFXHandle handle, const EColor		c );

		void		SetUniform		( EFXHandle handle, const EMatrix m );
		void		SetUniform		( EFXHandle handle, bool b );
		void		SetUniform		( EFXHandle handle, IPxTexture texture );
		void		CommitChanges	( void );

	protected:

		IPxEffect	effect;
		
		//	shader params :		
		EString		name;
		EString		texture_path0;
		EString		texture_path1;
		EString		texture_path2;
		EString		texture_path3;
		EString		texture_path4;
		EString		texture_path5;
		EString		injection;
		
		bool		is_solid;
		bool		is_translucent;
		bool		is_distortive;
		bool		is_emissive;
		bool		is_masked;

		struct {
			EFXHandle		is_solid;
			EFXHandle		is_translucent;
			EFXHandle		is_distortive;
			EFXHandle		is_emissive;
			EFXHandle		is_masked;
			
			EFXHandle		is_skinned;
			EFXHandle		joints;
		
			EFXHandle		random;
			EFXHandle		time;

			EFXHandle		entity_time;			
			EFXHandle		entity_color;
		
			EFXHandle		matrix_world;
			EFXHandle		matrix_view;
			EFXHandle		matrix_proj;
			
			EFXHandle		view_position;
			
			EFXHandle		texture0;
			EFXHandle		texture1;
			EFXHandle		texture2;
			EFXHandle		texture3;
			EFXHandle		texture4;
			EFXHandle		texture5;
		} uniforms;
		
		struct {
			EFXHandle	tech_fr_solid		;
			EFXHandle	tech_fr_shadows		;
			EFXHandle	tech_fr_emissive	;
			EFXHandle	tech_fr_translucent	;
			EFXHandle	tech_fr_distortive	;
			EFXHandle	tech_fr_wireframe	;
		} techniques;
		
		struct {
			bool			loaded;
			IPxTexture		texture0;
			IPxTexture		texture1;
			IPxTexture		texture2;
			IPxTexture		texture3;
			IPxTexture		texture4;
			IPxTexture		texture5;
		} resources;
	};